package com.ipay.wechat.factory;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ipay.wechat.component.Wechat;
import com.ipay.wechat.pojo.UserWechatInfo;
import com.ipay.wechat.service.UserWechatInfoService;
import com.ipay.wechat.util.MessageUtils;
import com.ipay.wechat.util.WechatUtil;

/**
 * 处理微信对接事件
 * @author Administrator
 *
 */
public class WechatFactory {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private HttpServletResponse response;
	//返回给用户的内容
	private String returnContent=null;
	//返回消息的格式
	private String msgType=null;
	//响应的xml消息
	private String responseXml=null;
	
	
	public WechatFactory(HttpServletResponse response){
		this.response=response;
	}
	public String dealWechatDockingEvents(String msgType, Map<String, String> map, Wechat wechat,UserWechatInfoService uwiService) throws IOException{
		switch (msgType) {
		case "text":
			responseXml=this.dealText(map);
			break;
		case "image":
			responseXml=this.dealImage(map);
			break;
		case "voice":
			responseXml=this.dealVoice(map);
			break;
		case "event":
			responseXml=this.dealEvent(map,wechat,uwiService);
			break;

		default:
			break;
		}
		return responseXml;
		
	}
	private String dealEvent(Map<String, String> map, Wechat wechat,UserWechatInfoService uwiService) throws IOException {
		// TODO Auto-generated method stub
		String eventType = map.get("Event");
		String eventKey = "";
		UserWechatInfo weInfo=new UserWechatInfo();
		logger.info("eventType==="+eventType);
		if (eventType.equalsIgnoreCase("click")){  //菜单点击事件
			eventKey = map.get("EventKey");
			logger.info(" eventKey="+eventKey);
			if(eventKey.equalsIgnoreCase("ArtificialSupport")){   
				  returnContent=URLDecoder.decode("亲，有什么可以帮助您的吗？如需要人工服务，请拨打客服电话 0731-89711722.","utf-8");
				  msgType = "text";
				  responseXml = MessageUtils.printXml(msgType, returnContent,map.get("FromUserName"), map.get("ToUserName"),response);
			}
			
		}else if(eventType.equalsIgnoreCase("subscribe")){  //用户关注事件
			
			WechatUtil wechatUtil=new WechatUtil();
			wechatUtil.managerUserInfo(wechat,map.get("FromUserName"),uwiService);
			returnContent="欢迎关注道合聚盈";
			//returnContent=URLDecoder.decode("欢迎关注道合聚盈","utf-8"); 
			msgType = "text";
			responseXml = MessageUtils.printXml(msgType, returnContent, map.get("FromUserName"), map.get("ToUserName"),response);
			
		}else if (eventType.equalsIgnoreCase("unsubscribe")){  //取消关注
			weInfo.setOpenid(map.get("FromUserName"));
			weInfo.setSubscribe(0);
			uwiService.updateWechatUserInfo(weInfo);
		}
		return responseXml;
	}
	private String dealVoice(Map<String, String> map) {
		// TODO Auto-generated method stub
		return null;
	}
	private String dealImage(Map<String, String> map) {
		// TODO Auto-generated method stub
		return null;
	}
	private String dealText(Map<String, String> map) throws IOException {
		String content  = map.get("Content");
		logger.info("  用户发送的内容==="+content);
		if("0".equals(content)){
			returnContent=URLDecoder.decode("如需要人工服务，请拨打客服电话 0731-89711722.","utf-8");
		}else{
			returnContent=URLDecoder.decode("亲，有什么可以帮助您的吗？如需要人工服务，请拨打客服电话 0731-89711722.","utf-8");
		}
		msgType = "text";
		responseXml = MessageUtils.printXml(msgType, returnContent, map.get("FromUserName"), map.get("ToUserName"),response);
		logger.info("responseXml=="+responseXml);
		return responseXml;
	}

}
